/**
  ******************************************************************************
  * @file           : route_common.h
  * @brief          : Header file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __ROUTE_H
#define __ROUTE_H

#include <stdint.h>
#include "tskcfg.h"
#include "route_str.h"
#include "route_485.h"
#include "route_gw.h"
#include "route_gwcfg.h"
#include "route_ec.h"
#include "route_dk.h"
#include "spiflash.h"

#ifndef ROUTE_VERSION
#define ROUTE_VERSION             50
#endif

#include "route_lx.h"
#include "route_cf.h"
#include "route_xc.h"

#define ROUTE_STORAGE_PARA_KEY    "ROTPAK"

#define ROUTE_INTEGRATED          1        /* 0为独立盒子, 1为集成环控 */

#define ROUTE_DEVICE_MAXNUM               4
#define ROUTE_TOPO_MAXNUM                 (ROUTE_DEVICE_MAXNUM + 2)
#define ROUTE_GET_TOPO_MAXNUM             16

#define ROUTE_NETTOLOC_QUE_LENGTH         20
#define ROUTE_NETTOLOC_QUE_ITEMSIZE       sizeof(Route_PayloadType)

#define ROUTE_EVEWTO485_QUE_LENGTH        20
#define ROUTE_EVEWTO485_QUE_ITEMSIZE      sizeof(Route_PayloadType)

#define ROUTE_EVEWTONET_QUE_LENGTH        25
#define ROUTE_EVEWTONET_QUE_ITEMSIZE      sizeof(Route_PayloadType)

#define ROUTE_FLASH_DEVICE_LIST           ((const Route_StorageDevlistType *)CFLASH_DEVLIST_ADDR)
#define Route_DeviceList                  (ROUTE_FLASH_DEVICE_LIST->dl)
#define ROUTE_SELF_DID                    (Route_DeviceList.gw.did)
#define ROUTE_SELF_TOKEN                  (Route_DeviceList.gw.token)

/* 设备列表 */
typedef struct
{
  char did[ROUTE_DID_BUFLEN];
  char token[ROUTE_TOKEN_BUFLEN];
}Route_DeviceType;

typedef struct
{
  Route_DeviceType gw;
  Route_DeviceType hk;
  Route_DeviceType dk[ROUTE_DEVICE_MAXNUM];
  Route_DeviceType lx[ROUTE_DEVICE_MAXNUM];
  Route_DeviceType cf[ROUTE_DEVICE_MAXNUM];
  Route_DeviceType xc[ROUTE_DEVICE_MAXNUM];
//  Route_DeviceType zl[ROUTE_DEVICE_MAXNUM];
//  Route_DeviceType sp[ROUTE_DEVICE_MAXNUM];
//  Route_DeviceType sw[ROUTE_DEVICE_MAXNUM];
//  Route_DeviceType sd[ROUTE_DEVICE_MAXNUM];
}Route_DeviceListType;


/* 拓扑列表 */
typedef struct
{
  uint32_t topo_num;
  char dids[ROUTE_GET_TOPO_MAXNUM][ROUTE_DID_BUFLEN];
}Route_ToPoListType;

/* 绑定和解绑列表 本地与网络同步用 */
typedef struct
{
  char did[ROUTE_DID_BUFLEN];
	char token[ROUTE_TOKEN_BUFLEN];
}Route_TopoSyncType;

typedef struct
{
  uint32_t num;
  Route_TopoSyncType item[ROUTE_GET_TOPO_MAXNUM];
}Route_TopoSyncListType;

/* 设备列表 存储结构体 */
typedef struct
{
  uint32_t crc;
	Route_DeviceListType dl;
}Route_StorageDevlistType;


extern Route_ToPoListType     Route_NetTopoList, Route_LocalTopoList;
extern Route_TopoSyncListType Route_BoundList, Route_UnboundList;

extern Route_PayloadType Route_to_net_pub_back;
extern Route_PayloadType Route_net_to_loc_back;

extern uint8_t Route_PayloadCRC(Route_PayloadType *rtpl, uint8_t type);
extern void Route_ConvertToTopolist(Route_ToPoListType *local);
extern const Route_DeviceType *Route_GetIndexInDevlist(Route_DevType_Type type, uint8_t index);
extern uint8_t Route_IsDidMatchDevice(const char *did, uint8_t *devindex);
extern void    Route_IsTopoMatchDevice(void);
extern uint8_t Route_RefreshDeviceList(const char *did);
extern uint8_t Route_token(const char *did, char *out, unsigned short out_max_len);
extern void Route_Init(void);

extern void Route_SaveDevlist(void);
extern uint8_t Route_CheckDevlist(void);

#endif /* __ROUTE_H */
